var app = getApp()
var http = app.require('utils/http.js')
var util = app.require('utils/util.js')
var validator = require('../../../utils/validator.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        CustomBar: app.globalData.CustomBar,
        
        listDatas: [],
        isPullDown: false,
        isPullUp: false,
        searchForm: {
            name_linkPhone_linkman: '',
        },
        pageInfo: {
            pageIndex: 1,//当前页
            pageCount: 0,// 总页数
            pageNumber: 10, // 每页显示条数
            sort: 'id desc',
            totalCount: 0, // 总记录数
            endFlag: false,
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        that.getListDatas(true)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    switchBar(event) {
        let tabbarIndex = event.detail
        wx.redirectTo({
          url: app.globalData.redirectPages[tabbarIndex],
          fail(e) {
            console.log(e)
           }
        })
    },

    ListTouchStart(e) {
        this.setData({
          ListTouchStart: e.touches[0].pageX
        })
      },
    
      // ListTouch计算方向
      ListTouchMove(e) {
        this.setData({
          ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
        })
      },
    
      // ListTouch计算滚动
      ListTouchEnd(e) {
        if (this.data.ListTouchDirection =='left'){
          this.setData({
            modalName: e.currentTarget.dataset.target
          })
        } else {
          this.setData({
            modalName: null
          })
        }
        this.setData({
          ListTouchDirection: null
        })
      },

    bindInput(e) {
        let dataset = e.currentTarget.dataset;
        let value = e.detail.value;
        if (!value) {
            value = e.detail
        }
        let index = dataset.index;
        if (index != undefined) {
          this.data[dataset.obj][index][dataset.item] = value;
          this.setData({
            [dataset.obj]: this.data[dataset.obj]
          })
        } else {
          this.data[dataset.obj][dataset.item] = value;
          this.setData({
            [dataset.obj]: this.data[dataset.obj]
          })
        }
      },

      callPhone(e) {
          let that = this
          let pohone = e.currentTarget.dataset.value
        wx.makePhoneCall({
          phoneNumber: pohone
        })
      },

      edit(e) {
        let that = this
        let id = e.currentTarget.dataset.value
        wx.navigateTo({
          url: '/pages/customer/edit/edit?id=' + id,
        })
      },

      delete(e) {
        let that = this;
        wx.showModal({
          content: '确认删除',
          success (res) {
            if (res.confirm) {
              let id = e.currentTarget.dataset.id;
              let params = {
                id: id
              };
              http.request('customer/delete', params, function(res) {
                that.reloadAllListDatas()
              });
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      },

    search(e) {
        this.getListDatas(true);
      }, 
    
      
      onPullDownRefresh(e) {
        var that = this
        if (that.data.isPullDown) {
            return;
        }
    
        that.setData({
          isPullDown: true
        });
        
        that.getListDatas(true, null, null, function() {
          that.setData({
            isPullDown: false
          });
          wx.stopPullDownRefresh();
        });
      },
    
      onReachBottom: function() {
        var that = this
        if (that.data.pageInfo.endFlag || that.data.isPullUp) {
            return;
        }
    
        that.setData({
          isPullUp: true
        });
        
        
        that.getListDatas(false, null, null, function() {
          that.setData({
            isPullUp: false
          });
        });
      },

    
      reloadAllListDatas() {
        let that = this;
        let beforePageInfo = util.deepClone(that.data.pageInfo);
        that.data.pageInfo.pageNumber = beforePageInfo.pageIndex * beforePageInfo.pageNumber
        
        that.setData({
          pageInfo: that.data.pageInfo
        })
    
        that.getListDatas(true, function() {
          that.data.pageInfo.pageIndex = beforePageInfo.pageIndex;
          that.data.pageInfo.pageNumber = beforePageInfo.pageNumber;
          that.setData({
            pageInfo: that.data.pageInfo
          })
          that.resetPageInfo();
        });
      },
    
      resetPageInfo() {
        let that = this;
        let pageInfo = that.data.pageInfo;
        let pageIndex = parseInt(pageInfo.pageIndex);
        let totalCount = parseInt(pageInfo.totalCount);
        let pageNumber = parseInt(pageInfo.pageNumber);
        let pageCount = totalCount / pageNumber + (totalCount % pageNumber == 0 ? 0 : 1);
        let endFlag = pageIndex >= pageCount ? true : false;
    
        let newPageInfo = {
          pageIndex: pageIndex,
          pageCount: pageCount,
          pageNumber: pageNumber,
          totalCount: totalCount,
          sort: pageInfo.sort,
          endFlag: endFlag
        }
    
        that.setData({
          pageInfo: newPageInfo
        })
      },
    
      getListDatas(refresh, successAct, failAct, complateAct) {
        let that = this;
        
        let params = Object.assign({}, that.data.searchForm);
        params['pageIndex'] = refresh ? 1 : (that.data.pageInfo.pageIndex + 1);
        params['pageNumber'] = that.data.pageInfo.pageNumber;
        params['sort'] = that.data.pageInfo.sort;
        console.log(params)
        http.request('customer/search', params, function(res) {
          var pageIndex = parseInt(res.pageIndex);
          var pageCount = parseInt(res.pageCount);
          var pageNumber = parseInt(res.pageNumber);
          var endFlag = pageIndex >= pageCount ? true : false;
          let datas = res.datas;
          
          let pageInfo = {
            pageIndex: pageIndex,
            totalCount: res.totalCount,
            pageCount: pageCount,
            pageNumber: pageNumber,
            sort: that.data.pageInfo.sort,
            endFlag: endFlag
          }
          that.setData({
            pageInfo: pageInfo
          })
    
          if (pageIndex == 1) {
            that.setData({
              listDatas: datas
            })
          } else {
            let tmpDatas = that.data.listDatas
            tmpDatas.push(...datas);
            that.setData({
                listDatas: tmpDatas
            })
          }
    
          typeof successAct == "function" && successAct(res.datas);
        }, function (res) {
          typeof failAct == "function" && failAct(res);
        }, function (res) {
          typeof complateAct == "function" && complateAct(res);
        }) 
      },
})